﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Jumpton.SFA.Server.BusinessRule.System;

/// <summary>
///PageBase 的摘要说明
/// </summary>
public class PageBase : Page
{

    private String serverPath;
    //获取后台地址的虚拟路径，在webconfig中配置
    public String ServerPath
    {
        get
        {
            try
            {
                serverPath = ConfigurationManager.AppSettings["ServerPath"];
            }
            catch (Exception)
            {
                return "";
            }
            return serverPath;
        }
    }


    public Boolean ReadOnly
    {
        get;
        set;
    }

    public String UserID
    {
        get;
        set;
    }


    private void PageBase_Init(object sender, EventArgs e)
    {
        //验证输入的参数是否完整，需要包括REQUESTKEY,SESSIONID,VERSIONID等
        String REQUESTKEY, SESSIONID, VERSION;
        REQUESTKEY = Request.QueryString[CommonString.REQUESTKEY];
        SESSIONID = Request.QueryString[CommonString.SESSIONID];
        VERSION = Request.QueryString[CommonString.VERSION];
        //
        SecurityPass = !(String.IsNullOrEmpty(REQUESTKEY) || String.IsNullOrEmpty(VERSION));
        if (!SecurityPass)
        {
            Response.Redirect(QueryStringHelper.GetRootURI(this.Context) + "/error.aspx?MESSAGE=" + "未经授权访问");
            return;
        }
        //验证安全验证是否通过
        String str = null;
        foreach (String key in Request.QueryString.Keys)
        {
            if (!key.ToUpper().Equals(CommonString.REQUESTKEY) && !key.ToUpper().Equals(CommonString.VERSION))
            {
                str += Request.QueryString[key];
            }
        }

        if (!Keys.SecurityKey.ContainsKey(VERSION) || Jumpton.Common.Functions.EncryptString(str + Keys.SecurityKey[VERSION]) != REQUESTKEY)
        {
            SecurityPass = false;

            //Response.Redirect(QueryStringHelper.GetRootURI(Context) + "/error.aspx?MESSAGE=" + "未经授权访问");
            Response.Redirect(QueryStringHelper.GetRootURI(Context) + "/error.aspx?MESSAGE=" + Request.QueryString.ToString());
        }



    }


    /// <summary>
    /// 获取安全验证是否通过
    /// </summary>
    public String VERSION
    {
        get
        {
            return Request.QueryString[CommonString.VERSION];


        }

    }


    /// <summary>
    /// 获取安全验证是否通过
    /// </summary>
    public bool SecurityPass
    {
        get;
        set;
    }








    public PageBase()
        : base()
    {
        // Thread.CurrentThread.CurrentUICulture = new CultureInfo(MyStateManager.Language);
        // Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(MyStateManager.Language);
        this.Init += new EventHandler(PageBase_Init);


    }








    public void PageBase_LoadComplete(object sender, EventArgs e)
    {
        ////如果需要进行验证
        //if (SecurityValid)
        //{
        //    string QueryString = Request.QueryString.ToString();
        //    int index = QueryString.IndexOf("Session=");
        //    if (index > -1)
        //    {
        //        ///截取查询字符串中需要进行本地验证的字符串
        //        QueryString = QueryString.Substring(0, index);
        //        QueryString = QueryString.Trim('&');

        //        string ValidCode = Jumpton.Common.Functions.EncryptString(QueryString + PrivateKey);
        //        string SessionCode = Request.QueryString["Session"];
        //        _err = true;
        //        if (ValidCode != SessionCode)
        //        {
        //            _err = false;
        //            EventHandler handler = (EventHandler)Events[_SecurityValidPass];
        //            if (handler != null)
        //            {
        //                handler(this, e);
        //            }
        //        }

        //    }
        //    else
        //    {
        //        _err = false;
        //        EventHandler handler = (EventHandler)Events[_SecurityValidPass];
        //        if (handler != null)
        //        {
        //            handler(this, e);
        //        }
        //    }
        //}
    }




}


